package apis

import (
    "fmt"

    "github.com/gin-gonic/gin"
    "github.com/go-admin-team/go-admin-core/sdk/api"
    "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
    _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

    "go-admin/app/hunter/models"
    "go-admin/app/hunter/service"
    "go-admin/app/hunter/service/dto"
    "go-admin/common/actions"
)

type HtContract struct {
    api.Api
}

// GetPage 获取HtContract列表
// @Summary 获取HtContract列表
// @Description 获取HtContract列表
// @Tags HtContract
// @Param talentId query string false "人才id"
// @Param notifyTime query time.Time false "提醒时间"
// @Param note query string false "备注信息"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.HtContract}} "{"code": 200, "data": [...]}"
// @Router /api/v1/ht-contract [get]
// @Security Bearer
func (e HtContract) GetPage(c *gin.Context) {
    req := dto.HtContractGetPageReq{}
    s := service.HtContract{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
    a := c.PostFormArray("notifyTime")
    _ = a

    p := actions.GetPermissionFromContext(c)
    list := make([]models.HtContract, 0)
    var count int64

    err = s.GetPage(&req, p, &list, &count)
    if err != nil {
        e.Error(500, err, fmt.Sprintf("获取HtContract失败，\r\n失败信息 %s", err.Error()))
        return
    }

    e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取HtContract
// @Summary 获取HtContract
// @Description 获取HtContract
// @Tags HtContract
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.HtContract} "{"code": 200, "data": [...]}"
// @Router /api/v1/ht-contract/{id} [get]
// @Security Bearer
func (e HtContract) Get(c *gin.Context) {
    req := dto.HtContractGetReq{}
    s := service.HtContract{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
    var object models.HtContract

    p := actions.GetPermissionFromContext(c)
    err = s.Get(&req, p, &object)
    if err != nil {
        e.Error(500, err, fmt.Sprintf("获取HtContract失败，\r\n失败信息 %s", err.Error()))
        return
    }

    e.OK(object, "查询成功")
}

// Insert 创建HtContract
// @Summary 创建HtContract
// @Description 创建HtContract
// @Tags HtContract
// @Accept application/json
// @Product application/json
// @Param data body dto.HtContractInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/ht-contract [post]
// @Security Bearer
func (e HtContract) Insert(c *gin.Context) {
    req := dto.HtContractInsertReq{}
    s := service.HtContract{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
    // 设置创建人
    req.SetCreateBy(user.GetUserId(c))

    err = s.Insert(&req)
    if err != nil {
        e.Error(500, err, fmt.Sprintf("创建HtContract失败，\r\n失败信息 %s", err.Error()))
        return
    }

    e.OK(req.GetId(), "创建成功")
}

// Update 修改HtContract
// @Summary 修改HtContract
// @Description 修改HtContract
// @Tags HtContract
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.HtContractUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/ht-contract/{id} [put]
// @Security Bearer
func (e HtContract) Update(c *gin.Context) {
    req := dto.HtContractUpdateReq{}
    s := service.HtContract{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }
    req.SetUpdateBy(user.GetUserId(c))
    p := actions.GetPermissionFromContext(c)

    err = s.Update(&req, p)
    if err != nil {
        e.Error(500, err, fmt.Sprintf("修改HtContract失败，\r\n失败信息 %s", err.Error()))
        return
    }
    e.OK(req.GetId(), "修改成功")
}

// Delete 删除HtContract
// @Summary 删除HtContract
// @Description 删除HtContract
// @Tags HtContract
// @Param data body dto.HtContractDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/ht-contract [delete]
// @Security Bearer
func (e HtContract) Delete(c *gin.Context) {
    s := service.HtContract{}
    req := dto.HtContractDeleteReq{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }

    // req.SetUpdateBy(user.GetUserId(c))
    p := actions.GetPermissionFromContext(c)

    err = s.Remove(&req, p)
    if err != nil {
        e.Error(500, err, fmt.Sprintf("删除HtContract失败，\r\n失败信息 %s", err.Error()))
        return
    }
    e.OK(req.GetId(), "删除成功")
}

// Count 查询沟通数量
// @Summary 查询沟通数量
// @Description 查询沟通数量
// @Tags HtContract
// @Success 200 {object} "{"code": 200, "message": "查询成功", "data":count}"
// @Router /api/v1/ht-contract/count/ [get]
// @Security Bearer
func (e HtContract) Count(c *gin.Context) {
    req := dto.HtContractGetPageReq{}
    s := service.HtContract{}
    err := e.MakeContext(c).
        MakeOrm().
        Bind(&req).
        MakeService(&s.Service).
        Errors
    if err != nil {
        e.Logger.Error(err)
        e.Error(500, err, err.Error())
        return
    }

    var count int64
    e.Orm.Table(models.HtContract{}.TableName()).Where("deleted_at is null").Count(&count)

    e.OK(count, "查询成功")
}
